package logic

import (
	"context"
	"time"

	"mall/service/backend/model"
	"mall/service/backend/rpc/internal/svc"
	"mall/service/backend/rpc/types/backend"

	"github.com/zeromicro/go-zero/core/logx"
)

type RemoveUserLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRemoveUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RemoveUserLogic {
	return &RemoveUserLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RemoveUserLogic) RemoveUser(in *backend.RemoveUserRequest) (*backend.RemoveUserResponse, error) {
	if len(in.Ids) == 0 {
		return &backend.RemoveUserResponse{}, nil
	}

	if in.Force > 0 {
		_, err := l.svcCtx.BackendUser.ForceDeleteBatch(l.ctx, in.Ids)
		if err != nil {
			return nil, err
		}
	} else {
		_, err := l.svcCtx.BackendUser.UpdateBatch(l.ctx, in.Ids, model.M{
			model.BackendUserColumns.Status:        uint16(in.Status),
			model.BackendUserColumns.LastUpdatedBy: uint64(in.UpdatedBy),
			model.BackendUserColumns.UpdatedAt:     time.Now(),
		})
		if err != nil && err != model.ErrNotUpdated {
			return nil, err
		}
	}

	return &backend.RemoveUserResponse{}, nil
}
